﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QianBuo.Lottery.Web.Areas.Manage.Models;
using Code.BLL;
using Code.Models;
using QianBuo.Lottery.Web.Areas.Manage.Models.Issue;
using QianBuo.Lottery.Web.Commen;

namespace QianBuo.Lottery.Web.Areas.Manage.Controllers
{
    [Authorize(Roles = "Manager_Lottery_Config")]
    public class IssueController : BaseController
    {
        public ActionResult Index(SearchIssue search,string msg=null)
        {
            if (!string.IsNullOrEmpty(msg))
                ViewData["msg"] = msg.AlertOffPage();
            if (search == null)
            {
                search = new SearchIssue();
            }
            if(search.PageInfo==null)
                search.PageInfo = new Web.Models.PageInfo();
            if (search.Search == null)
            {
                search.Search = new SearchIssue_Mod();
                search.Search.StartTime = DateTime.Now.Date;
                search.Search.EndTime = DateTime.Now.Date;
            }
            search.LotteryList = CacheManage.Lottery.Select(p => new SelectListItem {Selected=search.Search==null?false:search.Search.LotteryID==p.Value.ID, Text = p.Value.Name, Value = p.Value.ID.ToString() }).ToList();
            int sumCount;
            search.IssueList = new IssueManage().GetIssueList(search.Search, (search.PageInfo.CurrPage - 1) * search.PageInfo.PageCount, search.PageInfo.PageCount, out sumCount).ToList();
            search.IssueList.Each(p => p.LotteryName = CacheManage.Lottery[(LotteryType)p.LotteryID].Name);
            search.PageInfo.SunCount = sumCount;
            return View(search);
        }

        [HttpPost]
        [ActionName("Index")]
        [MultiButton(Name = "Search")]
        public ActionResult Index_Post([Bind(Exclude="PageInfo")]SearchIssue model)
        {
            return Index(model);
        }


        [HttpPost]
        [ActionName("Index")]
        [MultiButton(Name = "Add")]
        public ActionResult Index_Post3(SearchIssue model)
        {
            var msg = "";
            IssueManage im = new IssueManage();
            if (model.Search == null)
            {
                msg= "请选择您要添加期号的彩种";
                return Index(model,msg);
            }
            else if (!model.Search.LotteryID.HasValue)
            {
                msg = "请选择您要添加期号的彩种";
                return Index(model, msg);
            }
            else if (!(model.Search.StartTime.HasValue && model.Search.EndTime.HasValue))
            {
                msg = "请选择您要添加期号的开始时间和结束时间".AlertOffPage();
                return Index(model, msg);
            }
            var startDate=model.Search.StartTime.Value;
            var endDate = model.Search.EndTime.Value;
            if (startDate > endDate)
            {
                msg = "添加失败,开始时间不能大于结束时间。";
            }
            else
            {
                var cunt = im.AddIssue((LotteryType)model.Search.LotteryID, model.Search.StartTime.Value, (endDate - startDate).Days + 1);

                if (cunt > 0)
                    msg = ("成功添加" + cunt + "期");
                else if (cunt == 0)
                    msg = "您要添加的期号已存在。";
                else
                    msg = "添加失败";
            }
            return Index(model,msg);
        }

        [HttpPost]
        [ActionName("Index")]
        [MultiButton(Name = "Delete")]
        public ActionResult Index_Post2(SearchIssue model)
        {
            IssueManage im = new IssueManage();
            var msg = "";
            if (model.Search == null || model.Search.EndTime == null)
                msg = "请选择您要删除的期号的结束时间";
            else
            {
                var count = im.DeleteIssue(model.Search.LotteryID, model.Search.StartTime, model.Search.EndTime.Value);
                if (count > 0)
                    msg = "成功删除" + count + "条记录";
                else if (count == 0)
                    msg = "操作成功，但没有删除任何记录";
                else
                    msg = "操作失败!";
            }
            return Index(model, msg);
        }

        [HttpPost]
        [ActionName("Index")]
        [MultiButton(Name = "DeleteChose")]
        public ActionResult Index_Post4(SearchIssue model)
        {
            var msg = "";
            var idsStr = Request.Form["cbkIssueID"].Split(',');
            if (idsStr == null || idsStr.Length == 0)
                msg = "请选择您要删除的期号";
            else
            {
                var ids = idsStr.Select(p => { long l; if (long.TryParse(p, out l))return l; return -1; }).Where(p => p != -1);
                IssueManage im = new IssueManage();
                var count = im.DeleteIssue(ids.ToArray());
                if (count > 0)
                    msg = "成功删除" + count + "条记录";
                else if (count == 0)
                    msg = "操作成功！";
                else
                    msg = "删除失败!";
            }
            return Index(model, msg);
        }
    }
}
